import 'package:flutter/material.dart';

import 'package:webview_flutter/webview_flutter.dart';

import '../../generated/assets.dart';
import '../../generated/l10n.dart';

class TermOfUsWebWidget extends StatefulWidget {
  final con =
      WebViewController()..setJavaScriptMode(JavaScriptMode.unrestricted);

  TermOfUsWebWidget({super.key});

  @override
  State<TermOfUsWebWidget> createState() => _PrivacyWidgetState();
}

class _PrivacyWidgetState extends State<TermOfUsWebWidget> {
  final String customerServiceEmail = 'service@invoicenow.vip';
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    widget.con
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress:
              (progress) =>
                  print('=============== Loading progress: $progress%'),
          onPageStarted: (url) => print('=============== Page started: $url'),
          onPageFinished: (url) async {
            await widget.con.runJavaScript("window.location.hash = '#/terms';");
          },
          onNavigationRequest: (request) {
            print('=============== Navigating to: ${request.url}');
            return NavigationDecision.navigate;
          },
        ),
      )
      // ..loadHtmlString("${Assets.htmlIndex}#/policy");
      ..loadFlutterAsset(Assets.htmlIndex)
      ..addJavaScriptChannel(
        'nativeMethod', // H5 通过此对象调用方法
        onMessageReceived: (message) {
          if (message.message == 'toEmail') {
            print("==================toEmail================="); // 调用原生邮件逻辑
            // launchEmail(customerServiceEmail);
          }
        },
      );
  }

  // 打开邮件客户端
  // Future<void> launchEmail(String customerServiceEmail) async {
  //   final Uri emailUri = Uri(
  //     scheme: 'mailto',
  //     path: customerServiceEmail,
  //     queryParameters: {'subject': "11", 'body': "|1"},
  //   );
  //
  //   if (await canLaunchUrl(emailUri)) {
  //     await launchUrl(emailUri);
  //   } else {
  //     throw 'Could not launch email client.';
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('飞机')),
      body: WebViewWidget(controller: widget.con),
    );
  }
}
